export const DOMAIN_URL = 'https://wenzun.shilongtiyu.com'
export const BASE_URL = DOMAIN_URL + '/api'

import store from '../store'

export default function(options) { //传入的options是一个json对象
  return new Promise((resolve, reject) => {
    const token = store.state.token
    // uni.getStorageSync('Token')
    const header = options.header || {}
    header.Authorization = 'Bearer ' + token
    uni.request({
      url: BASE_URL + options.url,
      method: options.method || 'get',
      data: options.data || {},
      dataType: options.dataType || 'json',
      header,
      success: (res) => {
        if (res.data.status !== 0) {
          if (res.data.status === 30000) {
            uni.reLaunch({
              url: '/pages/login/index'
            })
            return
          }
          reject(res.data)
          return res.data
        }
        resolve(res.data)
      },
      fail: (err) => {
        uni.showToast({
          icon: 'none',
          title: "接口请求失败"
        })
        reject(err.data)
      }
    })
  })
}
